import java.util.PriorityQueue;
import java.util.Timer;
import java.util.TimerTask;

import static java.lang.System.currentTimeMillis;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-04-10
 * Time: 10:47
 */
/*
Timer内部有两个核心的成员：
TaskQueue类型的queue。
TimerThread类型的thread。
*/
class MyTimerTask implements Comparable<MyTimerTask> {
    private Runnable runnable;

    private long time;

    public MyTimerTask(Runnable runnable, long delay) {
        // delay 是一个相对的时间差. 形如 3000 这样的数值.
        // 构造 time 要根据当前系统时间和 delay 进行构造.
        time = System.currentTimeMillis() + delay;
        this.runnable = runnable;
    }
    public long getTime() {
        return time;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        // 认为时间小的, 优先级高. 最终时间最小的元素, 就会放到队首.
        // 怎么记忆, 这里是谁减去谁?? 不要记!! 记容易记错~~
        // 随便写一个顺序, 然后实验一下就行了.
        return (int) (this.time - o.time);
        // return (int) (o.time - this.time);
    }
}
class Mytimer{

        private PriorityQueue<MyTimerTask> priorityQueue = new PriorityQueue<>();
        Object loket =new Object();
    public void schedule(Runnable runnable, long delay) {
        synchronized (loket) {
            MyTimerTask task = new MyTimerTask(runnable, delay);
            priorityQueue.offer(task);
            // 每次来新的任务, 都唤醒一下之前的扫描线程. 好让扫描线程根据最新的任务情况, 重新规划等待时间.
            loket.notify();
        }
    }
        public Mytimer(){
            Thread thread = new Thread(()->{
                try {
                    while (true) {
                        synchronized (loket) {
                            while (priorityQueue.isEmpty()) {
                                loket.wait();
                            }
                            MyTimerTask mytimerTask = priorityQueue.peek();
                            long curtime = System.currentTimeMillis();
                            if (curtime >= mytimerTask.getTime()) {
                                priorityQueue.poll();
                                mytimerTask.getRunnable().run();
                            } else {
                                loket.wait(mytimerTask.getTime() - currentTimeMillis());
                            }
                        }
                    }
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
}
public class test_2 {

    public static void main(String[] args) {
        Mytimer timer= new Mytimer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("3000ms后");
            }
        }, 3000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("2000ms后");
            }
        }, 2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("1000ms后");
            }
        }, 1000);

    }
}
